package com.hx.controller;

import com.hx.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Objects;

@RestController
@RequestMapping("/user")
@Slf4j
public class Controller01 {

    @GetMapping("/hello")
    public String hello(Integer id) {
        log.info("id:{}",id);
        return "hello Controller";

    }

    @PostMapping(value = "/login",produces = "application/json;charset=UTF-8")
    public String login( String validateCode,String username , String password, HttpSession session){
       if (StringUtils.isEmpty(validateCode)) {
           return "验证码不能为空";
       }
       String sessionCode= (String) session.getAttribute("kaptcha");
       session.removeAttribute("kaptcha");
       if (!validateCode.equalsIgnoreCase(sessionCode)) {
           return "验证码错误";
       }
       if(StringUtils.isEmpty(username)|| StringUtils.isEmpty(password)){
            return "用户名密码不能为空";
        }
        if("jack".equals(username) && "111".equals(password)){
            User user=  new User();
            user.setUsername(username);
            user.setPassword(password);
            session.setAttribute("user",user);

            return username + "登陆成功";
        }
        return "用户名密码错误";
    }



}
